//
// Created by konglinglong on 2020/9/8.
//

#include <stdio.h>
#include <stdlib.h>
#include "../../include/osal/osMemory.h"

static volatile uint32_t gMemAllocSize = 0;
static volatile uint32_t gMemAllocCount = 0;
static volatile uint32_t gMemFreeCount = 0;

void* OS_MemCalloc(size_t num, size_t size)
{
    void* ptr;

    ptr = calloc(num, size);
    if (ptr)
    {
        gMemAllocSize += (num * size);
        gMemAllocCount++;
    }

    return ptr;
}

void* OS_MemMalloc(size_t size)
{
    void* ptr;

    ptr = malloc(size);
    if (ptr)
    {
        gMemAllocSize += size;
        gMemAllocCount++;
    }

    return ptr;
}

void* OS_MemRealloc(void *ptr, size_t size)
{
    return realloc(ptr, size);
}

void OS_MemFree(void* ptr)
{
    if (ptr)
    {
        gMemFreeCount++;
        free(ptr);
    }
}

uint32_t OS_MemGetAllocSize()
{
    return gMemAllocSize;
}

uint32_t OS_MemGetAllocCount()
{
    return gMemAllocCount;
}

uint32_t OS_MemGetFreeCount()
{
    return gMemFreeCount;
}

void OS_MemResetCounter()
{
    gMemAllocSize = 0;
    gMemAllocCount = 0;
    gMemFreeCount = 0;
}

void OS_MemDump(void* buffer, uint32_t size)
{
    snprintf(buffer, size, "OS_MemDump : AllocSize[%u], AllocCount[%u], FreeCount[%u].",
        gMemAllocSize, gMemAllocCount, gMemFreeCount);
}